﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using OgilvyOne.Model;
using OgilvyOne.BLL;
using System.IO;

public partial class admin_allergy_mama_mama_video_detail : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {

            if (Request.Params["id"] != null && Request.Params["id"] != "")
            {//修改
                DataTable dt = BLLArticleInfo.getMama(Request.Params["id"]);
                foreach (DataRow row in dt.Rows)
                {
                    this.mamaname.Text = row["mamaName"].ToString();
                    this.babyname.Text = row["babyName"].ToString();
                    this.address.Text = row["address"].ToString();
                    this.video_desc.Text = row["descc"].ToString();
                    this.videoname.Text = row["videoName"].ToString();
                    this.filename.Text = row["fileName"].ToString();
                    this.state.SelectedValue = row["status"].ToString();
                    this.id.Value = row["id"].ToString();
                }
            }
        }
    }

    protected void Sub_click(object sender, EventArgs e)
    {
        int id = 0;
        if (this.id.Value != null && this.id.Value != "")
        {
            id = int.Parse(this.id.Value);
        }
        try
        {
            if (id > 0)
            {//update
                this.id.Value = id.ToString();
                string mamaName = this.mamaname.Text;
                string babyName = this.babyname.Text;
                string address = this.address.Text;
                string desc = this.video_desc.Text;
                string videoName = this.videoname.Text;
                string file_name = this.filename.Text;
                string status = this.state.SelectedValue;

                if (FileUpload1.PostedFile.ContentLength != 0) //判断选取对话框选取的文件长度是否为0
                {
                    string strBaseLocation = Server.MapPath("/allergy/uploadfiles/flv/");
                    string temp_name = DateTime.Now.ToString("yyyymmddhhmmss").Trim() + FileUpload1.PostedFile.ContentLength.ToString();
                    string temp_ext = FileUpload1.FileName.Substring(FileUpload1.FileName.LastIndexOf("."), FileUpload1.FileName.Length - (FileUpload1.FileName.LastIndexOf(".")));
                    if (this.filename.Text == null && this.filename.Text == "")
                    {
                        file_name = temp_name + temp_ext;

                    }
                    else
                    {
                        file_name = this.filename.Text;
                    }
                    try
                    {
                        if (File.Exists(strBaseLocation + file_name))
                        {
                            File.Delete(strBaseLocation + file_name);
                        }

                        FileUpload1.PostedFile.SaveAs(strBaseLocation + file_name);

                    }
                    catch (Exception ex)
                    {
                        Response.Write("上传失败" + ex.ToString());
                    }
                    BLLArticleInfo.upMamaVideo("fileName", file_name, id.ToString());
                }
                else
                {
                    Response.Write("<script>alert('请选择文件');</script>");
                }
                if (FileUpload2.PostedFile.ContentLength != 0) //判断选取对话框选取的文件长度是否为0
                {
                    string strBaseLocation = Server.MapPath("~/allergy/uploadfiles/image/");
                    string temp_name = DateTime.Now.ToString("yyyymmddhhmmss").Trim() + FileUpload2.PostedFile.ContentLength.ToString();
                    string temp_ext = FileUpload2.FileName.Substring(FileUpload2.FileName.LastIndexOf("."), FileUpload2.FileName.Length - (FileUpload2.FileName.LastIndexOf(".")));
                    string pic = temp_name + temp_ext;
                    if (File.Exists(strBaseLocation + pic))
                    {
                        File.Delete(strBaseLocation + pic);
                    }
                    FileUpload2.PostedFile.SaveAs(strBaseLocation + pic);
                    BLLArticleInfo.upMamaVideo("pic", pic, id.ToString());
                }
                else
                {
                    Response.Write("<script>alert('请选择文件');</script>");
                }

                int flag2 = BLLArticleInfo.upMamaInfo(mamaName, babyName, address, desc, videoName, status,file_name, this.id.Value);
                if (flag2 > 0)
                {
                    Response.Redirect("mama_video_list.aspx?columnid=214&layerid=1,179,206,214");
                }
                else
                {
                    Response.Write("<script>alert('请选择文件');</script>");
                }



            }
            else
            {//add
                nestle_mama info = new nestle_mama();
                info.MamaName = this.mamaname.Text;
                info.BabyName = this.babyname.Text;
                info.Address = this.address.Text;
                info.Descc = this.video_desc.Text;
                info.VideoName = this.videoname.Text;
                info.Filename = this.filename.Text;
                info.Status = this.state.SelectedValue;

                if (FileUpload1.PostedFile.ContentLength != 0) //判断选取对话框选取的文件长度是否为0
                {
                    string strBaseLocation = Server.MapPath("~/allergy/uploadfiles/flv/");
                    string temp_name = DateTime.Now.ToString("yyyymmddhhmmss").Trim() + FileUpload1.PostedFile.ContentLength.ToString();
                    string temp_ext = FileUpload1.FileName.Substring(FileUpload1.FileName.LastIndexOf("."), FileUpload1.FileName.Length - (FileUpload1.FileName.LastIndexOf(".")));
                    string file_name = temp_name + temp_ext;
                    FileUpload1.PostedFile.SaveAs(strBaseLocation + file_name);
                    Response.Write("<script>alert('上传成功');</script>");
                    info.Filename = file_name;
                }
                else
                {
                    Response.Write("<script>alert('请选择文件');</script>");
                }
                if (FileUpload2.PostedFile.ContentLength != 0) //判断选取对话框选取的文件长度是否为0
                {
                    string strBaseLocation = Server.MapPath("~/allergy/uploadfiles/image/");
                    string temp_name = DateTime.Now.ToString("yyyymmddhhmmss").Trim() + FileUpload2.PostedFile.ContentLength.ToString();
                    string temp_ext = FileUpload2.FileName.Substring(FileUpload2.FileName.LastIndexOf("."), FileUpload2.FileName.Length - (FileUpload2.FileName.LastIndexOf(".")));
                    string file_name = temp_name + temp_ext;
                    if (File.Exists(strBaseLocation + file_name))
                    {
                        File.Delete(strBaseLocation + file_name);
                    }

                    FileUpload2.PostedFile.SaveAs(strBaseLocation + file_name);
                    Response.Write("<script>alert('上传成功');</script>");
                    info.Pic = file_name;
                }
                else
                {
                    Response.Write("<script>alert('请选择文件');</script>");
                }
                BLLArticleInfo.InsertMama(info);
            }
        }
        catch (Exception ex)
        {
            Response.Write(ex.Message);
            Response.End();
        }
        //if (isup) new CmsArticleBll().UpdateAudit(int.Parse(Request.Params["aid"]), GetSessionUsersModel().Username);
        Response.Redirect("mama_video_list.aspx?columnid=214&layerid=" + Request.Params["layerid"]);
    }
}